package org.linlinjava.litemall.core.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.github.wxpay.sdk.IWXPayDomain;
import com.github.wxpay.sdk.WXPayConfig;
import com.github.wxpay.sdk.WXPayConstants;
import com.mzwise.tunnel.service.TunnelService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

@Configuration
public class WxConfig {
    @Autowired
    private WxProperties properties;

    @Bean
    public WxMpDefaultConfigImpl wxMpConfig() {
        WxMpDefaultConfigImpl mpConfig = new WxMpDefaultConfigImpl();
        mpConfig.setAppId(properties.getSubMpAppid());
        mpConfig.setSecret(properties.getSubMpAppSecret());
        mpConfig.setToken(properties.getSubMpAppToken());
        mpConfig.setAesKey(properties.getSubMpAppAesKey());
        return mpConfig;
    }

    @Bean
    public WxMpService wxMpService(WxMpDefaultConfigImpl mpConfig) {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(mpConfig);
        return wxMpService;
    }

    @Bean
    public WxMaConfig wxMaConfig() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(properties.getSubAppid());
        config.setSecret(properties.getSubAppSecret());
        return config;
    }

    @Bean
    public WxMaService wxMaService(WxMaConfig maConfig) {
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(maConfig);
        return service;
    }

    @Bean
    public WxPayConfig wxPayConfig() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(properties.getAppId());
        payConfig.setSubAppId(properties.getSubAppid());
        payConfig.setMchId(properties.getMchId());
        payConfig.setSubMchId(properties.getSubMchId());
        payConfig.setMchKey(properties.getMchKey());
        payConfig.setNotifyUrl(properties.getNotifyUrl());
        payConfig.setKeyPath(properties.getKeyPath());
        payConfig.setTradeType("JSAPI");
        payConfig.setSignType("MD5");
        return payConfig;
    }

    @Bean
    public WxPayService wxPayService(WxPayConfig payConfig) {
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

    @Bean
    public WXPayConfig wxTransferConfig() {
        return new WXPayConfig() {
            @Override
            public String getAppID() {
                return properties.getSubAppid();
            }

            @Override
            public String getMchID() {
                return properties.getSubMchId();
            }

            @Override
            public String getKey() {
                return properties.getSubMchKey();
            }

            @Override
            public InputStream getCertStream() {
                String certPath = properties.getSubKeyPath();
                File file = new File(certPath);
                try {
                    InputStream certStream = new FileInputStream(file);
                    byte[] certData = new byte[(int) file.length()];
                    certStream.read(certData);
                    certStream.close();
                    ByteArrayInputStream certBis = new ByteArrayInputStream(certData);
                    return certBis;
                } catch (Exception err) {
                    System.out.println("读取证书错误");
                    return null;
                }
            }

            @Override
            public int getHttpConnectTimeoutMs() {
                return 8000;
            }

            @Override
            public int getHttpReadTimeoutMs() {
                return 10000;
            }

            @Override
            public IWXPayDomain getWXPayDomain() {
                IWXPayDomain iwxPayDomain = new IWXPayDomain() {
                    @Override
                    public void report(String domain, long elapsedTimeMillis, Exception ex) {
                    }

                    @Override
                    public DomainInfo getDomain(WXPayConfig config) {
                        return new IWXPayDomain.DomainInfo(WXPayConstants.DOMAIN_API, true);
                    }
                };
                return iwxPayDomain;
            }
        };
    }

    @Bean
    public TunnelService tunnelService(WXPayConfig wxTransferConfig) {
        TunnelService tunnelService = new TunnelService();
        // 微信企业转账配置
        tunnelService.setWxTransferConfig(wxTransferConfig);
        tunnelService.init();
        return tunnelService;
    }
}